﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FastReflectionLib;
using System.Web;
using SZHL.UI.AppCode;
using SZHL.Data;
using Newtonsoft.Json;
using System.Data;

namespace SZHL.UI.API
{
    public class LCSPManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(LCSPManage).GetMethod(msg.Action.ToUpper());
            LCSPManage model = new LCSPManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 流程审批添加
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDLCSP(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_LCSP lcsp = JsonConvert.DeserializeObject<ZH_LCSP>(P1);
            if (lcsp.Content.Trim() == "")
            {
                msg.ErrorMsg = "内容不能为空";
                return;
            }
            if (lcsp.LeiBie.Trim() == "")
            {
                msg.ErrorMsg = "请选择类别";
                return;
            }

            if (lcsp.ID == 0)
            {
                lcsp.CRDate = DateTime.Now;
                lcsp.CRUser = UserInfo.User.UserName;
                lcsp.BranchName = new JH_Auth_BranchB().GetBranchNameByCode(UserInfo.User.BranchCode);
                lcsp.ShenQingRen = string.IsNullOrEmpty(lcsp.ShenQingRen) ? UserInfo.User.UserRealName : lcsp.ShenQingRen;
                lcsp.SPStatus = "0";
                lcsp.SPStatus1 = "0";
                lcsp.BranchNo = UserInfo.User.BranchCode;
                int pdId = new Yan_WF_PDB().GetProcessID(lcsp.LeiBie);
                lcsp.intProcessStanceid = pdId;
                new ZH_LCSPB().Insert(lcsp);
            }
            else
            {
                new ZH_LCSPB().Update(lcsp);
            }
            msg.Result = lcsp;
        }
        /// <summary>
        /// 我填写的流程审批
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">类别</param>
        /// <param name="P2">审批内容</param>
        /// <param name="UserInfo"></param>
        public void GETMYLCSPLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " ";
            if (P1 != "")
            {
                strWhere += string.Format(" And LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And   Content like '%{0}%'", P2);
            }
            string strSQL = string.Format("select ShenQingRen,BranchName,LeiBie,Content,CRDate,CRUser,SPStatus,SPStatus1    from ZH_LCSP where CRUser='{0}' " + strWhere, UserInfo.User.UserName);
            DataTable dt = new Yan_WF_TDB().GetDTByCommand(strSQL);
            dt.Columns.Add("isforbid");
            foreach (DataRow row in dt.Rows)
            {
                int pdId = new Yan_WF_PDB().GetProcessID(row["LeiBie"].ToString());
                row["isforbid"] = new Yan_WF_TDB().GetTaskStatus(pdId);

            }
            msg.Result = dt;
        }
        /// <summary>
        /// 审批列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETSPLCSPLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " ";
            if (P1 != "")
            {
                strWhere += string.Format(" And LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And  Content like '%{0}%'", P2);
            }

            DataTable dt = new JFBXManage().GetSPList("ZH_LCSP", "ccxj.ShenQingRen,ccxj.ID,ccxj.BranchName,ccxj.LeiBie,ccxj.Content,ccxj.CRDate,ccxj.CRUser", strWhere, UserInfo.User.UserName);
            msg.Result = dt;
        }
        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">Id</param>
        /// <param name="P2">意见</param>
        /// <param name="UserInfo"></param>
        public void SPLCSP(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_LCSP lcsp = new ZH_LCSPB().GetEntity(d => d.ID.ToString() == P1);
            string status = context.Request["spstatus"]; //状态

            if (lcsp.SPStatus != "0")
            {
                lcsp.SPStatus1 = status;
                lcsp.SPYJ1 = P2;
                lcsp.SPTime1 = DateTime.Now;
                lcsp.SPUser1 = UserInfo.User.UserName;
            }
            else
            {
                lcsp.SPStatus = status;
                lcsp.SPYJ = P2;
                lcsp.SPTime = DateTime.Now;
                lcsp.SPUser = UserInfo.User.UserName;
            }
            new ZH_LCSPB().Update(lcsp);
            msg.Result = lcsp;
        }
        /// <summary>
        /// 获取审批列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">审批内容</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETLCSPLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = string.Format("SELECT ShenQingRen,BranchName,LeiBie,Content,CRDate,CRUser,SPStatus,SPStatus1,ID   from ZH_LCSP ");
            string strWhere = " Where 1=1 ";
            if (P1 != "")
            {
                strWhere += string.Format(" And LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And  Content like '%{0}%'", P2);
            }

            DataTable dt = new ZH_JFBXB().GetDTByCommand(strSql + strWhere + " order by crdate desc");
            dt.Columns.Add("isforbid");
            foreach (DataRow row in dt.Rows)
            {
                int pdId = new Yan_WF_PDB().GetProcessID(row["LeiBie"].ToString());
                row["isforbid"] = new Yan_WF_TDB().GetTaskStatus(pdId);

            }
            msg.Result = dt;
        }
        /// <summary>
        /// 获取审批信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETLCSPMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_LCSP lcsp = new ZH_LCSPB().GetEntity(d => d.ID.ToString() == P1);
            msg.Result = lcsp;
            int pdId = new Yan_WF_PDB().GetProcessID(lcsp.LeiBie);
            msg.Result1 = new Yan_WF_TDB().GetTaskStatus(pdId);
        }
        /// <summary>
        /// 删除请假出差
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELLCSPBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {

                if (new ZH_LCSPB().Delete(d => d.ID.ToString() == P1))
                {
                    msg.ErrorMsg = "";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
    }
}